package com.lihao.wanandroid.app.ext

import android.text.TextUtils
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.navigation.NavController
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.lifecycle.lifecycleOwner
import com.lihao.jetpackcore.ext.nav
import com.lihao.wanandroid.R
import com.lihao.wanandroid.app.util.CacheUtils
import java.io.BufferedReader
import java.io.FileReader
import java.io.IOException

/**
 * Created by lihao on 2020/6/10.
 * Describe:
 */

/**
 * 获取进程号对应的进程名
 *
 * @param pid 进程号
 * @return 进程名
 */
fun _getProcessName(pid: Int): String? {
    var reader: BufferedReader? = null
    try {
        reader = BufferedReader(FileReader("/proc/$pid/cmdline"))
        var processName = reader.readLine()
        if (!TextUtils.isEmpty(processName)) {
            processName = processName.trim { it <= ' ' }
        }
        return processName
    } catch (throwable: Throwable) {
        throwable.printStackTrace()
    } finally {
        try {
            reader?.close()
        } catch (exception: IOException) {
            exception.printStackTrace()
        }

    }
    return null
}

/**
 * 是否是刷新
 */
fun Int.isRefresh(): Boolean {
    return this == 0
}

/**
 * 拦截登陆操作，如果没有登录跳转登录，否则执行自己的逻辑
 */
fun NavController.jumpByLogin(
    action: (NavController) -> Unit
) {
    if (CacheUtils.isLogin()) {
        action(this)
    } else {
        this.navigate(R.id.action_to_loginFragment)
    }
}

/**
 * 在Activity中显示选择对话框
 */
fun AppCompatActivity.showMessage(
    title: String = "温馨提示",
    message: String,
    positiveButtonText: String = "确定",
    positiveButtonAction: () -> Unit = {},
    negativeButtonText: String = "",
    negativeButtonAction: () -> Unit = {}
) {
    MaterialDialog(this)
        .cancelable(false)
        .lifecycleOwner(this)
        .show {
            title(text = title)
            message(text = message)
            positiveButton(text = positiveButtonText) {
                positiveButtonAction.invoke()
            }
            if (negativeButtonText.isNotEmpty()) {
                negativeButton(text = negativeButtonText) {
                    negativeButtonAction.invoke()
                }
            }
        }
}

/**
 * 在Fragment中显示选择对话框
 */
fun Fragment.showMessage(
    title: String = "温馨提示",
    message: String,
    positiveButtonText: String = "确定",
    positiveButtonAction: () -> Unit = {},
    negativeButtonText: String = "",
    negativeButtonAction: () -> Unit = {}
) {
    activity?.let {
        MaterialDialog(it)
            .cancelable(false)
            .lifecycleOwner(this)
            .show {
                title(text = title)
                message(text = message)
                positiveButton(text = positiveButtonText) {
                    positiveButtonAction.invoke()
                }
                if (negativeButtonText.isNotEmpty()) {
                    negativeButton(text = negativeButtonText) {
                        negativeButtonAction.invoke()
                    }
                }
            }
    }
}